package com.seezoon.application.user.executor;

import com.seezoon.application.user.dto.GenerateCaptchaCmd;
import com.seezoon.application.user.dto.clientobject.GenerateCaptchaCO;
import com.seezoon.domain.service.user.CaptchaService;
import com.seezoon.domain.service.user.vo.CaptchaVO;
import com.seezoon.infrastructure.dto.Response;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

/**
 * 生成图形验证码
 */
@Slf4j
@RequiredArgsConstructor
@Component
@Validated
public class GenerateCaptchaCmdExe {

    /**
     * 图像验证码两分钟过期
     */
    private final static int defaultExpireSeconds = 60 * 2;
    private final CaptchaService captchaService;

    public Response<GenerateCaptchaCO> execute(@Valid @NotNull GenerateCaptchaCmd cmd) {
        CaptchaVO captchaVO = captchaService.generate(cmd.getTarget(), cmd.getType(), defaultExpireSeconds);
        GenerateCaptchaCO co = new GenerateCaptchaCO();
        co.setCaptchaImage(captchaVO.getCaptchaImage());
        co.setCaptchaToken(captchaVO.getCaptchaToken());
        return Response.success(co);
    }
}
